package com.example.projectsharedspace;

import java.util.List;
import java.util.TreeSet;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MsgAdapter extends ArrayAdapter<Message> {

	/** To cache views of item */
	private static class ViewHolder {
		private TextView text1;
		private TextView text2;
		private TextView text3;

		/**
		 * General constructor
		 */
		ViewHolder() {
			// nothing to do here
		}
	}

	/** Inflater for list items */
	private final LayoutInflater inflater;

	private static final int TYPE_MSG = 0;
	private static final int TYPE_POLL = 1;

	private TreeSet mSeparatorsSet = new TreeSet();

	/**
	 * General constructor
	 *
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 * @param objects
	 */
	public MsgAdapter(final Context context,
			final int resource,
			final int textViewResourceId,
			final List<Message> objects) {
		super(context, resource, textViewResourceId, objects);

		this.inflater = LayoutInflater.from(context);
	}


	@Override
	public View getView(final int position, final View convertView, final ViewGroup parent) {

		View itemView = convertView;
		ViewHolder holder = null;

		final Message item = getItem(position);


		if(null == itemView) {

			holder = new ViewHolder();

			itemView = this.inflater.inflate(R.layout.message_row, parent, false);

			holder.text1 = (TextView)itemView.findViewById(R.id.message);
			holder.text2 = (TextView)itemView.findViewById(R.id.timeDate);
			holder.text3 = (TextView)itemView.findViewById(R.id.user);

			itemView.setTag(holder);
		} else {
			holder = (ViewHolder)itemView.getTag();
		}


		holder.text1.setText(item.getMsg());
		holder.text2.setText(item.getTD().getTime().toString());
		holder.text3.setText(item.getUser().getDisplayname());

		return itemView;
	}
}